import React, { FC, useState, useEffect } from 'react'
import Taro from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { AtIcon } from 'taro-ui'
import api from '@/services/config'
import './index.scss'

interface Community {
  communityAddress: string
}
const mySettings = ['我的积分', '收货地址设置', '我的社区', '关于我们', '隐私']
const MySettings: FC = () => {
  const [community, setCommunity] = useState<Community>({
    communityAddress: ''
  })
  useEffect(() => {
    const userInfo = JSON.parse(Taro.getStorageSync('userInfo'))
    setCommunity(userInfo?.community)
  }, [])
  // 退出登录
  function handSetup() {
    api['user/logout']().then(() => {
      Taro.removeStorageSync('userInfo')
      Taro.removeStorageSync('token')
      Taro.reLaunch({ url: '/pages/Login/index' })
    })
  }
  function jump(i) {
    switch (i) {
      case 0:
        break
      case 1:
        Taro.navigateTo({ url: '/pages/Address/index' })
        break
      case 2:
        break
      case 3:
        break
      case 4:
        break
    }
  }
  return (
    <View className='mySettings'>
      <View className='mySettings-setList'>
        {mySettings.map((item, index) => {
          return (
            <View
              className='mySettings-setList__info  sb-flex b-bottom'
              key={index}
              onClick={() => {
                jump(index)
              }}
            >
              <View>{item}</View>
              <View>
                {index === 3 && <Text>{community.communityAddress}</Text>}
                <AtIcon prefixClass='icon' value='rightjt' size='15' color='#bbbbbb'></AtIcon>
              </View>
            </View>
          )
        })}
      </View>
      <Button className='commmon-btn' onClick={handSetup}>
        退出登录
      </Button>
    </View>
  )
}

export default MySettings
